﻿using Common;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingFormSystem.FallRiskAssessment
{
    public partial class FallRiskAssessmentPrint : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        DateTime Start;
        DateTime End;
        ElectronicSignature es;
        #endregion

        #region 构造函数
        public FallRiskAssessmentPrint(Tb_User loginUser, PatientInfo patientInfo, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            Start = StartDate;
            End = EndDate;
            es = new ElectronicSignature(this.LoginUser);
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\住院患儿跌倒风险评估表.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(this.PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.FallFRA, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    Fallfar ps = XmlUtil.XMLToObject<Fallfar>(record1.NursingFormData, Encoding.UTF8);
                    string image1 = "";
                    string RYHS = ps.PingGuRen;
                    if (!string.IsNullOrEmpty(RYHS))
                    {
                        image1 = es.GetElectricSignBase64(record1.AreaId, ps.PingGuRen);
                    }
                    DataRow row = dt.NewRow();
                    row["NURSE_CELL_NAME"] = PatientInfo.NURSE_CELL_NAME;//NURSE_CELL_NAME,
                    row["BED_NO"] = PatientInfo.BED_NO;//BED_NO,
                    row["PATIENTNAME"] = PatientInfo.PATIENT_NAME;
                    row["SEX"] = PatientInfo.SEX;//SEX
                    row["AGE"] = PatientInfo.AGE;//AGE
                    row["PATIENT_ID"] = PatientInfo.PATIENT_ID;//PATIENTID
                    row["DEPARTMENT_NAME"] = PatientInfo.DEPARTMENT_NAME;//DEPARTMENT_NAME
                    row["AssessmentDate1"] = ps.RiQi;
                    row["nianling1"] = ChkStr(ps.NianLing, 4) && ps.NianLing.Split('#')[0].Equals("1") ? 1 : 0;
                    row["nianling2"] = ChkStr(ps.NianLing, 4) && ps.NianLing.Split('#')[1].Equals("1") ? 1 : 0;
                    row["nianling3"] = ChkStr(ps.NianLing, 4) && ps.NianLing.Split('#')[2].Equals("1") ? 1 : 0;
                    row["nianling4"] = ChkStr(ps.NianLing, 4) && ps.NianLing.Split('#')[3].Equals("1") ? 1 : 0;
                    row["xingbie1"] = ChkStr(ps.XingBie, 2) && ps.XingBie.Split('#')[0].Equals("1") ? 1 : 0;
                    row["xingbie2"] = ChkStr(ps.XingBie, 2) && ps.XingBie.Split('#')[1].Equals("1") ? 1 : 0;
                    row["zhenduan1"] = ChkStr(ps.ZhenDuan, 4) && ps.ZhenDuan.Split('#')[0].Equals("1") ? 1 : 0;
                    row["zhenduan2"] = ChkStr(ps.ZhenDuan, 4) && ps.ZhenDuan.Split('#')[1].Equals("1") ? 1 : 0;
                    row["zhenduan3"] = ChkStr(ps.ZhenDuan, 4) && ps.ZhenDuan.Split('#')[2].Equals("1") ? 1 : 0;
                    row["zhenduan4"] = ChkStr(ps.ZhenDuan, 4) && ps.ZhenDuan.Split('#')[3].Equals("1") ? 1 : 0;
                    row["renzhizhangai1"] = ChkStr(ps.RenZhiZhangAi, 3) && ps.RenZhiZhangAi.Split('#')[0].Equals("1") ? 1 : 0;
                    row["renzhizhangai2"] = ChkStr(ps.RenZhiZhangAi, 3) && ps.RenZhiZhangAi.Split('#')[1].Equals("1") ? 1 : 0;
                    row["renzhizhangai3"] = ChkStr(ps.RenZhiZhangAi, 3) && ps.RenZhiZhangAi.Split('#')[2].Equals("1") ? 1 : 0;
                    row["huanjingyinsu1"] = ChkStr(ps.HuanJingYinSu, 4) && ps.HuanJingYinSu.Split('#')[0].Equals("1") ? 1 : 0;
                    row["huanjingyinsu2"] = ChkStr(ps.HuanJingYinSu, 4) && ps.HuanJingYinSu.Split('#')[1].Equals("1") ? 1 : 0;
                    row["huanjingyinsu3"] = ChkStr(ps.HuanJingYinSu, 4) && ps.HuanJingYinSu.Split('#')[2].Equals("1") ? 1 : 0;
                    row["huanjingyinsu4"] = ChkStr(ps.HuanJingYinSu, 4) && ps.HuanJingYinSu.Split('#')[3].Equals("1") ? 1 : 0;
                    row["duishoushudanying1"] = ChkStr(ps.DuiShouShuZhenJingMaZuiDeFanYing, 3) && ps.DuiShouShuZhenJingMaZuiDeFanYing.Split('#')[0].Equals("1") ? 1 : 0;
                    row["duishoushudanying2"] = ChkStr(ps.DuiShouShuZhenJingMaZuiDeFanYing, 3) && ps.DuiShouShuZhenJingMaZuiDeFanYing.Split('#')[1].Equals("1") ? 1 : 0;
                    row["duishoushudanying3"] = ChkStr(ps.DuiShouShuZhenJingMaZuiDeFanYing, 3) && ps.DuiShouShuZhenJingMaZuiDeFanYing.Split('#')[2].Equals("1") ? 1 : 0;
                    row["yaowuyingyong1"] = ChkStr(ps.YaoWuYingYong, 3) && ps.YaoWuYingYong.Split('#')[0].Equals("1") ? 1 : 0;
                    row["yaowuyingyong2"] = ChkStr(ps.YaoWuYingYong, 3) && ps.YaoWuYingYong.Split('#')[1].Equals("1") ? 1 : 0;
                    row["yaowuyingyong3"] = ChkStr(ps.YaoWuYingYong, 3) && ps.YaoWuYingYong.Split('#')[2].Equals("1") ? 1 : 0;
                    row["zongdefen"] = ps.ZongDeFen;
                    row["pingguren"] = ps.PingGuRen;
                    row["ElectricSignBase64"] = image1;
                    dt.Rows.Add(row);
                }
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENTNAME", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("AssessmentDate1", typeof(string));
            dt.Columns.Add("nianling1", typeof(string));
            dt.Columns.Add("nianling2", typeof(string));
            dt.Columns.Add("nianling3", typeof(string));
            dt.Columns.Add("nianling4", typeof(string));
            dt.Columns.Add("xingbie1", typeof(string));
            dt.Columns.Add("xingbie2", typeof(string));
            dt.Columns.Add("zhenduan1", typeof(string));
            dt.Columns.Add("zhenduan2", typeof(string));
            dt.Columns.Add("zhenduan3", typeof(string));
            dt.Columns.Add("zhenduan4", typeof(string));
            dt.Columns.Add("renzhizhangai1", typeof(string));
            dt.Columns.Add("renzhizhangai2", typeof(string));
            dt.Columns.Add("renzhizhangai3", typeof(string));
            dt.Columns.Add("huanjingyinsu1", typeof(string));
            dt.Columns.Add("huanjingyinsu2", typeof(string));
            dt.Columns.Add("huanjingyinsu3", typeof(string));
            dt.Columns.Add("huanjingyinsu4", typeof(string));
            dt.Columns.Add("duishoushudanying1", typeof(string));
            dt.Columns.Add("duishoushudanying2", typeof(string));
            dt.Columns.Add("duishoushudanying3", typeof(string));
            dt.Columns.Add("yaowuyingyong1", typeof(string));
            dt.Columns.Add("yaowuyingyong2", typeof(string));
            dt.Columns.Add("yaowuyingyong3", typeof(string));
            dt.Columns.Add("zongdefen", typeof(string));
            dt.Columns.Add("pingguren", typeof(string));
            dt.Columns.Add("ElectricSignBase64", typeof(string));//电签
        }
        #endregion

        #region 窗体加载启动锐浪报表
        private void FallRiskAssessmentPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
    }
}
